var e = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
        var n, o = arguments[t];
        for (n in o) Object.prototype.hasOwnProperty.call(o, n) && (e[n] = o[n]);
    }
    return e;
}, t = require("../api/user"), n = a(require("../store/index")), o = a(require("../libs/x/index")), r = a(require("./qs/index")), i = a(require("./storage"));

function a(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

var u = require("../siteinfo"), f = function() {
    return new Promise(function(e, n) {
        o.default.login().then(function(n) {
            n = n.code, (0, t.loginWithCode)(n).then(function(t) {
                i.default.setItem("session_key", t.session_key), i.default.setItem("openid", t.openid), 
                e(t.user);
            });
        }).catch(n);
    });
}, c = [ "", " ", "  ", "   ", "    ", "     ", "      ", "       ", "        ", "         " ], s = function(e, t, n) {
    if ((t -= (e += "").length) <= 0) return e;
    if (n || 0 === n || (n = " "), " " == (n += "") && t < 10) return c[t] + e;
    for (var o = ""; (t >>= 1) && (1 & t && (o += n), t >>= 1); ) n += n;
    return o + e;
}, l = function(e) {
    var t = e + "";
    return 1 == e.toString().length && (t = "0" + t), t;
}, d = [ "周日", "周一", "周二", "周三", "周四", "周五", "周六" ], g = {
    wxLogin: function() {
        return new Promise(function(e, t) {
            f().then(function(t) {
                n.default.data.userInfo = t, e(t);
            }).catch(function(e) {
                i.default.clear("openid"), t(e);
            });
        });
    },
    fillPath: function(e) {
        var t = /https?:\/\//.test(e);
        return /^\/\//.test(e) || t ? e : /https?:\/\/[a-z0-9.]+\//.exec(u.siteroot)[0] + e;
    },
    leftPad: s,
    leftpad: function(e) {
        var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : 2, n = (n = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : "0") || "0";
        if (e) {
            for (e = e.toString(); e.length < t; ) e = n + e;
            return e;
        }
        return "00";
    },
    convertSecond: function(e) {
        if (e) {
            var t = s(Math.round(e % 60), 2, "0");
            return s(Math.floor(e / 60), 2, "0") + ":" + t;
        }
        return "00:00";
    },
    queryString: r.default,
    getUserMobile: function(e, r) {
        return new Promise(function(a, u) {
            o.default.checkSession().then(function() {
                var o = i.default.getItem("session_key"), u = i.default.getItem("openid");
                (0, t.decodeUserMobile)({
                    openid: u,
                    session_key: o,
                    iv: r,
                    encrypted: e
                }).then(function(e) {
                    e = e.mobile, n.default.data.userInfo.mobile = e, a(e);
                });
            }).catch(function(o) {
                u(o), f().then(function(o) {
                    var i = getItem("openid");
                    (0, t.decodeUserMobile)({
                        openid: i,
                        session_key: o,
                        iv: r,
                        encrypted: e
                    }).then(function(e) {
                        e = e.mobile, n.default.data.userInfo.mobile = e, a(e);
                    }).catch(u);
                });
            });
        });
    },
    toastText: function(e) {
        var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : 2e3;
        return new Promise(function(n, o) {
            wx.showToast({
                title: e,
                icon: "none",
                duration: t,
                success: n,
                fail: o
            });
        });
    },
    modalText: function(t) {
        var n = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "我知道了", o = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : {};
        return new Promise(function(r, i) {
            "string" != typeof t && (t = JSON.stringify(t)), wx.showModal(e({
                content: t,
                showCancel: !1,
                confirmText: n
            }, o, {
                success: function(e) {
                    r(e);
                },
                fail: function(e) {
                    i(e);
                }
            }));
        });
    },
    alwaysBack: function() {
        var e = 0 < arguments.length && void 0 !== arguments[0] ? arguments[0] : "redirectTo", t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "/pages/index/index";
        return new Promise(function(n, o) {
            wx.navigateBack({
                delta: 1,
                success: n,
                fail: function(n) {
                    wx[e]({
                        url: t
                    }), o(n);
                }
            });
        });
    },
    after: function(e) {
        e && "function" == typeof e && setTimeout(function() {
            e();
        }, 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : 800);
    },
    getCurrentDate: function() {
        var e = Date.parse(new Date());
        return (e = new Date(e)).getFullYear() + "/" + (e.getMonth() + 1 < 10 ? "0" + (e.getMonth() + 1) : e.getMonth() + 1) + "/" + (e.getDate() < 10 ? "0" + e.getDate() : e.getDate());
    },
    getDateType: function(e) {
        var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : "", n = 2 < arguments.length && void 0 !== arguments[2] && arguments[2];
        return e = parseInt(e), e = new Date(e), "year" === t ? e.getFullYear() : "month" !== t ? "date" === t ? e.getDate() < 10 ? "0" + e.getDate() : e.getDate() : "week" === t ? n ? e.getDay() : d[e.getDay()] : l(e.getMonth() + 1) + "/" + l(e.getDate()) : (e = e.getMonth() + 1) < 10 ? "0" + e : e;
    },
    calendarDateList: function(e, t) {
        var n = [];
        e && t || (e = (o = new Date()).getFullYear(), t = o.getMonth() + 1);
        for (var o = new Date(e, t - 1, 1).getDay(), r = new Date(e, t, 0).getDate(), i = new Date(e, t - 1, 0).getDate(), a = o, u = 0; u < 42; u++) {
            var f = u - a + 1, c = e, s = t, l = f, d = !0;
            f <= 0 ? (d = !1, --s, l = i + f) : r < f && (d = !1, s += 1, l -= r), s <= 0 && (s = 12, 
            --c), 13 === s && (c += s = 1), n.push({
                index: f,
                year: c,
                month: s,
                date: l,
                isCurrent: d,
                fullDate: c + "-" + (s < 10 ? "0" + s : s) + "-" + (l < 10 ? "0" + l : l)
            });
        }
        return n;
    }
};

module.exports = g;